package cn.layou.thread;

import java.util.concurrent.*;

/**
 * 线程创建的方法
 * 1. new MyThread().start()
 * 2. new Thread(r).start()
 * 3 new Thread(() -> System.out.print());
 * 4. ThreadPool
 * 5. Callable Future FutureTask
 *
 * @author layou
 * @date 2021-04-15
 */
public class T01_ThreadCreate {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new MyThread().start();

        new Thread(new MyRunnable()).start();

        new Thread(() -> System.out.println("lambda run")).start();

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(() -> System.out.println("thread pool run"));

        Future<String> future = executorService.submit(new MyCallable());
        //阻塞
        String result = future.get();
        System.out.println(result);

        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask).start();
        //阻塞
        System.out.println(futureTask.get());
    }


}

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("my thread run");
    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("my runnable run");
    }
}

class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        return "success";
    }
}